﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Linq;
namespace DienThoai
{
	/// <summary>
	/// Interaction logic for CustomerInfoxaml.xaml
	/// </summary>
	public partial class CustomerInfoxaml : Window
	{
		public CustomerInfoxaml()
		{
			this.InitializeComponent();
			
			// Insert code required on object creation below this point.
		}
        DienThoaiDataContext db = new DienThoaiDataContext();
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            
            lbMaKH.Content = getID();
            foreach (var i in db.Kinds)
            {
                cbbType.Items.Add(i.Detail);
            }
            cbbType.SelectedIndex = 0;
        }
        private string getID()
        {
            string ID = "";

            var query = from i in db.Customers
                        select i.PersonID;

            if (query.Count() == 0)
                ID = "KH00001";
            else
            {
                int temp = 0 ;
                foreach (var i in query)
                    temp = int.Parse(i.ToString().Substring(2)) + 1;
                if (temp < 10)
                    ID = "KH0000" + temp.ToString();
                else if(temp <100)
                    ID = "KH000" + temp.ToString();
                else if (temp < 1000)
                    ID = "KH00" + temp.ToString();
                else if (temp < 10000)
                    ID = "KH0" + temp.ToString();
                else 
                    ID = "KH" + temp.ToString();
            }
            return ID;
        }
        private void Cancel_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void btNewCustomer_Click(object sender, RoutedEventArgs e)
        {
            Person CommonInfo = new Person();
            Customer DetailInfo = new Customer();
            float number ;
            if (txtFullName.Text == "")
                MessageBox.Show("Bạn chưa điền họ tên.");
            else if (txtAddress.Text == "")
                MessageBox.Show("Bạn chưa điền địa chỉ.");
            else if (dpDOB.SelectedDate == null)
                MessageBox.Show("Bạn chưa chọn ngày sinh.");

            else if ((float.TryParse(txtPhone.Text, out number)) == false && txtPhone.Text !="")
                MessageBox.Show("Số điện thoại có chứa ký tự. Xin nhập lại.");
            else
            {
                //Thông tin chung
                CommonInfo.PersonID = lbMaKH.Content.ToString();
                CommonInfo.Fullname = txtFullName.Text;
                CommonInfo.DOB = dpDOB.SelectedDate;
                CommonInfo.Address = txtAddress.Text;
                CommonInfo.Phone = txtPhone.Text;
                db.Persons.InsertOnSubmit(CommonInfo);
                //Thông tin chi tiết của Khách hàng
                var query = from k in db.Kinds
                            where k.Detail == cbbType.SelectedValue.ToString()
                            select k.KindID;

                foreach (var i in query)
                    DetailInfo.KindID = i;
                DetailInfo.PersonID = lbMaKH.Content.ToString();
                DetailInfo.Date = DateTime.Today;
                db.Customers.InsertOnSubmit(DetailInfo);
                db.SubmitChanges();
                MessageBox.Show("Khách hàng " + CommonInfo.Fullname + " đã thêm vào CSDL.");
                DialogResult = true;
            }
        }
	}
}